package cn.tedu.sims.aop;

import cn.tedu.sims.mapper.LogLoginMapper;
import cn.tedu.sims.mapper.UserMapper;
import cn.tedu.sims.pojo.dto.UserLoginDTO;
import cn.tedu.sims.pojo.entity.UserLogLogin;
import cn.tedu.sims.pojo.vo.UserLoginInfoVO;
import cn.tedu.sims.web.ServiceCode;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


@Aspect
@Component
public class LogAspect {
    @Autowired
    private LogLoginMapper logLoginMapper;
    @Autowired
    private UserMapper userMapper;
    //普通登陆成功
    @AfterReturning(value = "execution(* *..controller.*.Login(..))")
    public void LoginYes(JoinPoint pjp){
        String s = Arrays.toString(pjp.getArgs());
        String substring = s.substring(s.indexOf("username="), s.indexOf(","));
        String[] split = substring.split("=");

        int i = userMapper.countByUserName(split[1]);
        if (i!=0){
            //获取ip
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes()).getRequest();
            //添加登陆日志
            UserLogLogin user=new UserLogLogin();
            user.setUsername(split[1]);
            user.setLoginTime(LocalDateTime.now());
            user.setState(1);
            user.setIp(request.getRemoteAddr());
            logLoginMapper.insert(user);
        }

    }
    //普通登陆失败
    @AfterThrowing(value = "execution(* *..controller.*.Login(..))")
    public void LoginNo(JoinPoint pjp){
        String s = Arrays.toString(pjp.getArgs());
        String substring = s.substring(s.indexOf("username="), s.indexOf(","));
        String[] split = substring.split("=");

        int i = userMapper.countByUserName(split[1]);
        if (i!=0){
            //获取ip
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes()).getRequest();
            //添加登陆日志
            UserLogLogin user=new UserLogLogin();
            user.setUsername(split[1]);
            user.setLoginTime(LocalDateTime.now());
            user.setState(0);
            user.setIp(request.getRemoteAddr());
            logLoginMapper.insert(user);
        };

    }
    //邮箱登陆成功
    @AfterReturning(value = "execution(* *..controller.*.LoginByEmail(..))")
    public void LoginByEmailYes(JoinPoint pjp){
        String s = Arrays.toString(pjp.getArgs());
        String substring = s.substring(s.indexOf("email="), s.indexOf(","));
        String[] split = substring.split("=");

        UserLoginInfoVO byEmail = userMapper.getByEmail(split[1]);
        if (byEmail!=null){
            //获取ip
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes()).getRequest();
            UserLogLogin user=new UserLogLogin();
            user.setUsername(byEmail.getUsername());
            user.setLoginTime(LocalDateTime.now());
            user.setState(3);
            user.setIp(request.getRemoteAddr());
            logLoginMapper.insert(user);
        }
    }
    //邮箱登陆失败
    @AfterThrowing(value = "execution(* *..controller.*.LoginByEmail(..))")
    public void LoginByEmailNo(JoinPoint pjp){
        String s = Arrays.toString(pjp.getArgs());
        String substring = s.substring(s.indexOf("email="), s.indexOf(","));
        String[] split = substring.split("=");

        UserLoginInfoVO byEmail = userMapper.getByEmail(split[1]);
        if (byEmail!=null){
            //获取ip
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes()).getRequest();
            UserLogLogin user=new UserLogLogin();
            user.setUsername(byEmail.getUsername());
            user.setLoginTime(LocalDateTime.now());
            user.setState(2);
            user.setIp(request.getRemoteAddr());
            logLoginMapper.insert(user);
        }
    }
    //手机登陆成功
    @AfterReturning(value = "execution(* *..controller.*.LoginByPhone(..))")
    public void LoginByPhoneYes(JoinPoint pjp){
        String s = Arrays.toString(pjp.getArgs());
        String substring = s.substring(s.indexOf("phone="), s.indexOf(","));
        String[] split = substring.split("=");

        UserLoginInfoVO byPhone = userMapper.getByPhone(Long.valueOf(split[1]));
        if (byPhone!=null){
            //获取ip
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes()).getRequest();
            UserLogLogin user=new UserLogLogin();
            user.setUsername(byPhone.getUsername());
            user.setLoginTime(LocalDateTime.now());
            user.setState(5);
            user.setIp(request.getRemoteAddr());
            logLoginMapper.insert(user);
        }
    }
    //手机登陆失败
    @AfterThrowing(value = "execution(* *..controller.*.LoginByPhone(..))")
    public void LoginByPhoneNo(JoinPoint pjp){
        String s = Arrays.toString(pjp.getArgs());
        String substring = s.substring(s.indexOf("phone="), s.indexOf(","));
        String[] split = substring.split("=");

        UserLoginInfoVO byPhone = userMapper.getByPhone(Long.valueOf(split[1]));
        if (byPhone!=null){
            //获取ip
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes()).getRequest();
            UserLogLogin user=new UserLogLogin();
            user.setUsername(byPhone.getUsername());
            user.setLoginTime(LocalDateTime.now());
            user.setState(4);
            user.setIp(request.getRemoteAddr());
            logLoginMapper.insert(user);
        }
    }
}

